Ninja Database Pro
CreateIndexQuery<T,TIndex>(String,TIndex) Method
Example 



KellermanSoftware.NinjaDatabasePro Namespace > NinjaDbPro Class > CreateIndexQuery Method : CreateIndexQuery<T,TIndex>(String,TIndex) Method
Find index records for the specified single property index that match exactly the specified value. If the index is unique, this is the fastest way to find a record.
Syntax
Public Overloads Function CreateIndexQuery
    (Of T As {Class, New},
     TIndex)( _
   ByVal indexName As String, _
   ByVal valueToFind As TIndex _
) As List(Of SingleIndexRecord(Of T,TIndex))
Dim instance As NinjaDbPro
Dim indexName As String
Dim valueToFind As TIndex
Dim value As List(Of SingleIndexRecord(Of T,TIndex))
 
value = instance.CreateIndexQuery(Of T, TIndex)(indexName, valueToFind)
public List<SingleIndexRecord<T,TIndex>> CreateIndexQuery<T,TIndex>( 
   string indexName,
   TIndex valueToFind
)
where T: class, new()
public:
List<SingleIndexRecord<T^,TIndex^>^>^ CreateIndexQuerygeneric<typename T>
generic<typename TIndex>
( 
   String^ indexName,
   TIndex^ valueToFind
) 
where T: ref class, gcnew()

Parameters

indexName
valueToFind

Type Parameters

T
TIndex
Example
NinjaDbPro db = new NinjaDbPro("SingleIndexExampleDir", "SingleIndexExampleDir.db");
 
//Licensed Mode
//db.UserName = "John Smith 101224";
//db.LicenseKey = "aousdf832jasf==";
 
//Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
//db.Storage = new MemoryDatabase(); //In memory database
//db.Storage = new FileDatabase();  
 
//Open the database
db.OpenDatabase();
 
//Add a single property index if it doesn't already exist
if (!db.IndexExists<Person>("NameIndex"))
    db.AddIndex<Person,string>("NameIndex", "Name", IndexStyle.NonUnique);
 
//Add a record to the database
Person person = new Person();
person.Name = "John Smith";
person.DateCreated = DateTime.Now;
db.Save(person);
 
//Find an exact matach.  If the index is unique, this is the fastest.
var exactQuery = db.CreateIndexQuery<Person, string>("NameIndex", "John Smith");
 
//Lazy load the first record and write out a property
Console.WriteLine(exactQuery[0].LazyValue.DateCreated);
 
//Find a partial match
var partialQuery = db.CreateIndexQuery<Person, string>("NameIndex").Where(o => o.Index.StartsWith("John"));
 
//Write the DateCreated for all records that have a name beginning with John
foreach (var indexRecord in partialQuery)
{
    Console.WriteLine(indexRecord.LazyValue.DateCreated);
}
 
//Close the database
db.CloseDatabase();
'INSTANT VB NOTE: This code snippet uses implicit typing. You will need to set 'Option Infer On' in the VB file or set 'Option Infer' at the project level.
 
Dim db As New NinjaDbPro("SingleIndexExampleDir", "SingleIndexExampleDir.db")
 
'Licensed Mode
'db.UserName = "John Smith 101224";
'db.LicenseKey = "aousdf832jasf==";
 
'Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
'db.Storage = new MemoryDatabase(); //In memory database
'db.Storage = new FileDatabase();  
 
'Open the database
db.OpenDatabase()
 
'Add a single property index if it doesn't already exist
If Not db.IndexExists(Of Person)("NameIndex") Then
    db.AddIndex(Of Person,String)("NameIndex", "Name", IndexStyle.NonUnique)
End If
 
'Add a record to the database
Dim person As New Person()
person.Name = "John Smith"
person.DateCreated = Date.Now
db.Save(person)
 
'Find an exact matach.  If the index is unique, this is the fastest.
Dim exactQuery = db.CreateIndexQuery(Of Person, String)("NameIndex", "John Smith")
 
'Lazy load the first record and write out a property
Console.WriteLine(exactQuery(0).LazyValue.DateCreated)
 
'Find a partial match
Dim partialQuery = db.CreateIndexQuery(Of Person, String)("NameIndex").Where(Function(o) o.Index.StartsWith("John"))
 
'Write the DateCreated for all records that have a name beginning with John
For Each indexRecord In partialQuery
    Console.WriteLine(indexRecord.LazyValue.DateCreated)
Next indexRecord
 
'Close the database
db.CloseDatabase()
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

NinjaDbPro Class
NinjaDbPro Members
Overload List